package com.kanshu.ksgb.fastread.doudou.module.book.adapter;

import android.content.Context;
import android.text.TextUtils;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.MMKVUserManager;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.manager.CacheManager;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.ReaderJumpConfig;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.ReaderInputParams;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.RecentBookInfo;
import com.kanshu.ksgb.fastread.doudou.module.reader.utils.SettingManager;

import java.util.List;

/**
 * Title:
 * Description:
 * Copyright: Copyright(c)2018
 * Company: 看书网
 * CreateTime: 2018/5/14 14:47
 *
 * @author EvanHe
 * @version 3.0
 */
public class BookShelfRecentReadAdapter extends BaseQuickAdapter<RecentBookInfo> {

    public BookShelfRecentReadAdapter(Context context, List<RecentBookInfo> data) {
        super(context, data);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_shelf_recent_read_layout;
    }

    @Override
    protected void convert(BaseViewHolder holder, RecentBookInfo item, int position) {
        holder.setText(R.id.book_title, item.book_title);
        holder.setText(R.id.author, "本书作者：" + item.author_name);
        GlideImageLoader.load(item.cover_url, holder.getView(R.id.cover));
        String order = item.c_order;
        if (SettingManager.getInstance().isReadBook(item.book_id)) {
            order = String.valueOf(SettingManager.getInstance().getCurReadProgress(item.book_id).chapter);
        }
        holder.setText(R.id.book_progress, "已阅读至" + order + "章");
        holder.getConvertView().setOnClickListener(v -> {
            ReaderInputParams params = new ReaderInputParams();
            params.book_id = item.book_id;
            if (!SettingManager.getInstance().isReadBook(item.book_id)) {
                params.order = item.c_order;
                params.content_id = item.content_id;
            }
            ReaderJumpConfig.startReaderActivity(mContext, params);
        });
        int download_count = MMKVUserManager.getInstance().getBookDownloadChapterCount(item.book_id);
        if(download_count == 0){
            DisplayUtils.gone(holder.getView(R.id.download_chapter_count));
        }else{
            DisplayUtils.visible(holder.getView(R.id.download_chapter_count));
            holder.setText(R.id.download_chapter_count,  "已下载"  + (TextUtils.equals(String.valueOf(download_count), item.chapter_count) ? "" : download_count + "章"));
        }
    }
}
